package com.actionsky.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.actionsky.dao.NewsActiveDAO;
import com.actionsky.entity.NewsActive;
import com.actionsky.entity.RssItem;

/**
 * Servlet implementation class NewsFeed
 */
public class NewsFeed extends HttpServlet {
	private static final long serialVersionUID = 1L;
	List<NewsActive> newsList = null;
	
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		/** 订阅最新10条新闻 **/
		newsList = NewsActiveDAO.rssNewsFeed(10);
		try{
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/xml;charset=UTF-8");
			PrintWriter pw = response.getWriter();
			pw.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
			pw.println("<rss xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" version=\"2.0\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:atom=\"http://www.w3.org/2005/Atom\">");
			pw.println("<channel>");
			pw.println("<title>MySQL行业及公司新闻</title>");
			pw.println("<link>http://www.actionsky.com/news/news-active</link>");
			pw.println("<description>新发布的MySQL行业及公司的新闻为最近10篇</description>");
			pw.println("<language>zh-CN</language>");
			pw.println("<copyright>Copyright &#x00A9; 2011 Action Tech</copyright>");
			for(NewsActive newsActive : newsList){
				pw.println("<item>");
				pw.println("<title>" + newsActive.getNews_title().trim() + "</title>");
				pw.println("<link>http://www.actionsky.com/news/show-news?id=" + newsActive.getId() + "</link>");
				pw.println("<description>" + newsActive.getNews_abstract().trim() + "</description>");
				pw.println("<pubDate>" + newsActive.getNews_time() + "</pubDate>");
				pw.println("</item>");
			}
			pw.println("</channel>");
			pw.println("</rss>");
			pw.close();
		}catch(IOException e){
			e.printStackTrace();
		}
	}

	public void init() throws ServletException {
	}
	public void destroy() {
		super.destroy(); 
	}

}
